<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
Use App\Model\Category;
use App\Model\Goods;
use DB,Session;

class GoodsController extends CommonController
{
    // 商品列表页
    public function GoodsList(Request $request, $id = 0)
    {
        $data = $request->only('sort','q');
        // 获取分类信息
        if($id == 0){
            $cateName = '全部';
        } else {
            $cateName = Category::where('id', $id)->pluck('cate_name');
        }
        // 获取商品分类
        $cateInfo = Category::where('pid', $id)
                    ->where('cate_status', '1')
                    ->get();
        $category = new Category();
        $crumb = $category->getCrumb($id);
        // 获取当前分类及子分类的所有商品
        $goodsList = Goods::GoodsList($id,20,$data);
        return view('home.goods.list', compact('cateName', 'cateInfo', 'goodsList','crumb'));
    }
    
    // 商品详情
    public function GoodsItem($id)
    {
        // 商品基本信息
        $goodsInfo = DB::table('goods')->where('id', $id)
            ->where('goods_status', 1)
            ->where('is_sale', 1)
            ->first();
        
        // 验证商品是否存在
        if(false == $goodsInfo){
            return redirect('/tips', ['info'=>'商品不存在或者商品已下架', 'url'=>url('/list')]);
        }
        
        // 获取商品属性信息
        $goodsAttr = DB::table('goods_attr')->where('goods_id', $id)->get();
        
        // 获取商品图片信息
        $goodsAlbum = DB::table('goods_album')->where('goods_id', $id)->get();
        
        // 判断当前用户是否收藏该商品
        $islike = false;
        if(Session::has('userData')){
            $user_id = Session::get('userData')->id;
            $islike = DB::table('user_collection')->where('user_id', $user_id)
                    ->where('goods_id', $id)
                    ->pluck('id');
        }
        
        $comments = $this->showComment($id);        
        // 调用模板页面
        return view('home.goods.item', compact('goodsInfo', 'goodsAttr', 'goodsAlbum', 'islike', 'comments'));
    }
    
    // 获取商品属性
    public function getGoodsAttr($gid,$aid)
    {
        $r = ['status'=>0, 'attr'=>''];
        $attr = DB::table('goods_attr')->where('id', $aid)
                ->where('goods_id', $gid)
                ->first();
        if($attr){
            $r['status'] = 1;
            $r['info'] = $attr;
        }
        return response()->json($r);
    }
    
    // 获取商品数量
    public function getNum($id)
    {
        $r = ['status' => 0, 'info'=>''];
        $num = DB::table('goods')->where('id', $id)
                ->where('goods_status', 1)
                ->pluck('goods_num');
        if($num >= 0){
            $r['status'] = 1;
            $r['info'] = $num;
        } else {
            $r['info'] = '商品不存在或者已下架';
        }
        return response()->json($r);
    }
    
    // 显示商品评价
    public function showComment($gid)
    {
        // 先获取评论信息
        $comments = DB::table('comment')->leftJoin('comment_reply', 'comment.id', '=', 'comment_reply.comment_id')
                ->where('comment.comment_status', 1)
                ->where('comment.goods_id', $gid)
                ->orderBy('comment.id', 'desc')
                ->paginate(10);
        // 获取用户信息
        foreach($comments as $c){
            $c->user = DB::table('user')->where('id', $c->user_id)->select(['user_nickname', 'user_avartar'])->first();
        }
        
        return $comments;
    }
}
